package com.example.boss.pinkdiary;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boss.db.gen.DaoMaster;
import com.example.boss.db.gen.DaoSession;
import com.example.boss.db.gen.diaryPwd;
import com.example.boss.db.gen.diaryPwdDao;

/**
 * Created by 打好boss on 2018/6/18.
 */

public class registerDiary extends Activity {
    private EditText mViewEmail, mViewMailFrom, mViewMailPwd;
    private TextView mViewCode;
    private String emailTo;//收件人邮箱地址
    private String mailFrom;//发件人邮箱地址
    private String mailPwd;//发件人授权码
    private ImageView plane,go;
    private EditText send_code,psw_et;
    private String code,edit_code="",psw="";
    // GreenDAO使用的变量
    private SQLiteDatabase db1;
    private DaoMaster daoMaster1;
    private DaoSession daoSession1;
    private diaryPwdDao dpd;
    private diaryPwd dp;



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.colorPrimary_two);//通知栏所需颜色
        }
        setContentView(R.layout.register_diary);
        InitDAO_pwd();
        initView();
        planeListener();
        pswListener();
        sendCodeListener();
        goListener();
    }

    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
    private void initView() {
        mViewEmail = (EditText) findViewById(R.id.email_et);
        mailFrom = "boss19961229@163.com";
        mailPwd = "1234qwer";
        plane = (ImageView) findViewById(R.id.send);
        go=(ImageView) findViewById(R.id.go);
        send_code = (EditText) findViewById(R.id.send_code);
        psw_et=(EditText) findViewById(R.id.psw_et);
    }
    private void InitDAO_pwd() {
        DaoMaster.DevOpenHelper helper1 = new DaoMaster.DevOpenHelper(this, "diary_Pwd.db", null);
        db1 = helper1.getWritableDatabase();
        daoMaster1 = new DaoMaster(db1);//代表着一个数据库的连接
        daoSession1 = daoMaster1.newSession();//可以让我们使用一些Entity的基本操作和获取Dao操作类
        dpd = daoSession1.getDiaryPwdDao();
    }
    public void planeListener() {
        plane.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                emailTo = mViewEmail.getText().toString();
                char[] c = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
                StringWidthWeightRandom random = new StringWidthWeightRandom(c);
                code = random.getNextString(6);
                final String content = "PinkDiary发来了:" + code ;
                if(emailTo.equals("")){
                    Toast.makeText(registerDiary.this, "邮箱不得为空", Toast.LENGTH_LONG).show();
                }
                else{
                    Thread thread = new Thread() {
                        @Override
                        public void run() {
                            EmailUtil.getInstance().sendEmail(mailFrom, mailPwd, emailTo, "发给你啦~", content);//耗时操作，必须在线程中进行
                            //线程中弹出Toast
                            Looper.prepare();
                            Toast.makeText(registerDiary.this, "验证码已发送", Toast.LENGTH_LONG).show();
                            Looper.loop();
                        }
                    };
                    thread.start();
                }

            }
        });
    }
    public void pswListener(){//监听密码框
        psw_et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                psw=psw_et.getText().toString();
            }
        });
    }
    public void sendCodeListener() {//监听验证码输入框
        send_code.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                edit_code=send_code.getText().toString();

            }
        });
    }
    public void goListener(){//监听注册按钮
        go.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(!edit_code.equals(code)){
                    Toast.makeText(registerDiary.this, "验证码输入错误", Toast.LENGTH_LONG).show();
                    send_code.setText("");
                }
                else{
                    if(psw.equals("")){
                        Toast.makeText(registerDiary.this, "密码不得为空", Toast.LENGTH_LONG).show();
                    }
                    else{
                        diaryPwd saveList=new diaryPwd();
                        saveList.setEmail(emailTo);
                        saveList.setPwd(psw);
                        dpd.insert(saveList);
                        Intent intent = new Intent(registerDiary.this, PinkMain.class);
                        startActivity(intent);
                        finish();
                    }
                }
            }
        });
    }
}
